﻿using System;
using Epm.AppLogic.Framework;
using msBsc.Xsd.DataTds;
using System.Data;

namespace msBsc.AppLogic.DataAlc
{
    public class CBizServiceLable : CustBizObject
    {
        private CDmDjkServiceLable m_CDmDjkServiceLable = new CDmDjkServiceLable();
        private CDmDjkServiceLable oDmDjkServiceLable
        {
            get { return m_CDmDjkServiceLable; }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public CBizServiceLable()
            : base()
        {
            this.DataObj = new CDbAccess();
            this.DataObj.Init(this.m_CDmDjkServiceLable.DJK_SERVICE_LABLE);
            this.TableName = this.DataObj.TableName;
            this.DataObj.KeyField = this.DataObj.Fields["SERVICE_LABLE_ID"];
        }

        /// <summary>
        /// 新增记录前的处理
        /// </summary>
        /// <param name="drw">新增数据行</param>
        /// <returns>true/false</returns>
        /// <remarks>
        /// 生成ID值
        /// </remarks>
        protected override bool BeforeRowAdded(DataRow drw)
        {
            if (drw[this.KeyFieldName].ToString().Length < 10)
            {
                drw[this.KeyFieldName] = this.DataObj.NewID();
            }
            return true;
        }

        /// <summary>
        /// 记录修改前检验
        /// </summary>
        /// <param name="drw">当前记录</param>
        /// <returns>>校验合法，返回值为true,否则返回false</returns>
        /// <remarks>
        /// ID不允许修改,并把此修改操作记录到BSE_MODIFY_HISTORY表中
        /// </remarks>
        protected override bool BeforeRowModified(DataRow drw)
        {
            //ID不容许修改
            if (drw[this.KeyFieldName].ToString() != drw[this.KeyFieldName, DataRowVersion.Original].ToString())
            {
                Exception e = new Exception(this.TableName + "表的主键" + this.KeyFieldName + "字段不允许被修改");
                throw e;
            }
            return true;
        }
        protected override bool BeforeRowDeletedR(DataRow drw)
        {
            //using (msBsc.AppLogic.DataAlc.CBizShpPageColumn objtem = new CBizShpPageColumn())
            //{
            //    DataSet ds = objtem.SelectByPageNo(this.GetRowStr(drw, KeyFieldName));
            //    for (int i = ds.Tables[0].Rows.Count - 1; i >= 0; i--)
            //    {
            //        ds.Tables[0].Rows[i].Delete();
            //    }
            //    objtem.Update(ds);
            //} 
            return true;
        }

        protected override bool AfterRowAdded(DataRow drw)
        {
            return true;
        }

        protected override bool AfterRowModified(DataRow drw)
        {
            return true;
        }

        protected override bool AfterRowDeletedR(DataRow drw)
        {
            return true;
        }

        /// <summary>
        /// 获取服务标签名称
        /// </summary>
        /// <param name="djackId">地接客编号</param>
        /// <returns>获取的结果</returns>
        public int[] GetLabelName(string djackId)
        {
            if (string.IsNullOrEmpty(djackId))
            {
                return null;
            }
            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("DJKID", ParameterDirection.Input, EDbType.String, 36, djackId.Replace("'", "").Replace(",", "','")));
            DataSet ds = this.DataObj.QueryBySQL(@"SELECT SERVICE_LABLE_ID FROM DJK_SERVICE_LABLE WHERE DJACK_ID=@DJKID", parmCollection);
            //判断是否有数据
            ds.Tables[0].TableName = this.TableName;
            int rowCount = ds.Tables[0].Rows.Count;
            if (rowCount < 0)
            {
                return null;
            }
            int[] labelList = new int[rowCount];
            //转换数据
            for (int i = 0; i < rowCount; i++)
            {
                labelList[i] = Convert.ToInt32(ds.Tables[0].Rows[i]["SERVICE_LABLE_ID"]);
            }
            return labelList;
        }

        /// <summary>
        /// 添加地接客标签
        /// </summary>
        /// <param name="serviceLable">标签</param>
        /// <param name="djackId">地接客ID</param>
        /// <returns></returns>
        public string[] InsertDjackServiceLabel(int[] serviceLable, string djackId)
        {
            if (string.IsNullOrEmpty(djackId))
            {
                return new string[] { "FALSE", "地接客ID不能为空" };
            }
            DataSet ds = new CDmDjkServiceLable();
            for (int i = 0; i < serviceLable.Length; i++)
            {
                DataRow dr = ds.Tables[0].NewRow();
                dr["SERVICE_LABLE_ID"] = this.DataObj.NewID();
                dr["DJACK_ID"] = djackId;
                dr["LABLE_ID"] = serviceLable[i];
                dr["LABLE_IS_DELETED"] = 0;
                dr["LABLE_IS_DISPLAY"] = 1;
                dr["SERVICE_LABLE_RMK"] = null;
                ds.Tables[0].Rows.Add(dr);
            }
            CBizServiceLable lable = new CBizServiceLable();
            lable.Update(ds);

            return new string[] { "TRUE", "地接客添加标签成功" };
        }
       
    }
}
